package com.neusoft.nep.supervise.controller;

import com.neusoft.nep.common.Dialog;
import com.neusoft.nep.common.FileUtils;
import com.neusoft.nep.common.ViewSwitcher;
import com.neusoft.nep.supervise.entity.Grid;
import com.neusoft.nep.common.SessionManager;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SelectGridViewController {
    @FXML
    private ChoiceBox<String> cmbProvince;
    @FXML
    private ChoiceBox<String> cmbCity;
    @FXML
    private TextField txt_address;
    @FXML
    private Button btn_grid;

    private Map<String, List<String>> provinceMap = new HashMap<>();

    public void initialize() {
        provinceMap.put("辽宁省", List.of("沈阳市", "大连市", "铁岭市", "抚顺市"));
        provinceMap.put("山西省", List.of("太原市", "吕梁市", "大同市", "运城市"));
        provinceMap.put("湖南省", List.of("长沙市", "湘潭市", "衡阳市", "怀化市"));
        provinceMap.put("吉林省", List.of("长春市", "吉林市", "四平市"));
        cmbProvince.setItems(FXCollections.observableArrayList(provinceMap.keySet()));
        cmbProvince.getSelectionModel().clearSelection();
        cmbProvince.getSelectionModel().selectedItemProperty().addListener(
                (observable, oldValue, newValue) -> {
                    if (newValue != null) {
                        List<String> cities = provinceMap.get(newValue);
                        ObservableList<String> cityList = FXCollections.observableArrayList(cities);
                        cmbCity.setItems(cityList);
                        cmbCity.getSelectionModel().clearSelection();
                    }
                });
    }

    @FXML
    public void saveGrid() {
        String province = cmbProvince.getValue();
        String city = cmbCity.getValue();
        String address = txt_address.getText();
        if (province == null || city == null || address.isEmpty()) {
            Dialog.showDialog("网格地址提交", "请完整填写所有必填项！");
            return;
        }
        String id = generateNewId();
        Grid g1 = new Grid(id, province, city, address);
        Map<String, Grid> gridMap = FileUtils.readMapObject("grid.txt");
        gridMap.put(id, g1);
        //构建一个文件输出流
        FileUtils.writeMapObject("grid.txt", gridMap);

        SessionManager.setCurrentGridId(id);

        ViewSwitcher.switchView(btn_grid, "../supervise/view/SelectAQIView.fxml");
    }

    private String generateNewId() {
        Map<String, Grid> gridMap = FileUtils.readMapObject("grid.txt");
        if (gridMap.isEmpty()) {
            return "1"; // 如果文件为空，id从1开始
        }
        int maxId = 0;
        for (String key : gridMap.keySet()) {
            try {
                int currentId = Integer.parseInt(key);
                if (currentId > maxId) {
                    maxId = currentId;
                }
            } catch (NumberFormatException e) {
                // 如果键不是数字，忽略
            }
        }
        return String.valueOf(maxId + 1);
    }
}
